#include <stdlib.h>
#include <time.h>

#include <pc.h>
#include <dos.h>
#include <dpmi.h>

#include "speaker.h"
#include "timer.h"

void speaker_on() {
	outportb(SPEAKER_CTRL, inportb(SPEAKER_CTRL) | SPEAKER_ENABLE | TIMER2_ENABLE);
}

void speaker_off() {
	outportb(SPEAKER_CTRL, inportb(SPEAKER_CTRL) & ~SPEAKER_ENABLE & ~TIMER2_ENABLE);
}

void play_note(note_t *note) {
  if (note->freq == 0)
    msleep(note->dur);
  else {
    timer_load(TIMER_2, (TIMER_CLK / note->freq));
    speaker_on();
    msleep(note->dur);
    speaker_off();
  }
}

